package com.groupware.cal.mybatis;

import java.io.IOException;
import java.io.Reader;


import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.groupware.cal.vo.CalVo;

public class CalManager {

	  private static SqlSessionFactory sqlMapper;
	  
	   static {
	      try {
	         Reader reader = Resources
	               .getResourceAsReader("com/groupware/cal/mybatis/SqlMapConfig.xml");
	         sqlMapper = new SqlSessionFactoryBuilder().build(reader);
	         reader.close();
	      } catch (IOException e) {
	         throw new RuntimeException(
	               "Something bad happened while building the SqlMapClient instance."
	                     + e, e);
	      }
	   }
	public static List<CalVo> getCal(String userId) {
		SqlSession session = sqlMapper.openSession();		
		List<CalVo> list =session.selectList("getCal", userId);
		session.close();
		return list;
	}
	public static void insertCal(CalVo vo){
		SqlSession session = sqlMapper.openSession();
		session.insert("insertCal", vo);
		session.commit();
		System.out.println(vo);
		session.close();
	}
	public static void updateCal(CalVo vo){
		SqlSession session = sqlMapper.openSession();
		session.update("updateCal", vo);
		session.commit();
		System.out.println(vo);
		session.close();
	}
	public static void deleteCal(CalVo vo){
		SqlSession session = sqlMapper.openSession();
		session.delete("deleteCal", vo);
		session.commit();
		System.out.println(vo);
		session.close();
	}
	
	public static CalVo getUpdateCal(String title) {
		SqlSession session = sqlMapper.openSession();		
		CalVo vo =session.selectOne("getUpdateCal", title);
		session.close();
		return vo;
	}
	
	

}
